//
//  ContentView.swift
//  longshortmemory
//
//  Created by dgx on 2025/3/20.
//

import SwiftUI

struct ContentView: View {
    @StateObject private var viewModel = ChatViewModel()
    @State private var isWelcomeShown = true
    @State private var showModelConfig = false {
        didSet {
            print("ContentView中showModelConfig变为: \(showModelConfig)")
        }
    }
    
    var body: some View {
        ZStack {
            // 主要内容
            VStack(spacing: 0) {
                HStack(spacing: 0) {
                    // 左侧对话列表面板
                    ConversationListView(viewModel: viewModel)
                    
                    Divider()
                        .background(Color.gray.opacity(0.2))
                    
                    // 中间聊天面板
                    ChatView(viewModel: viewModel, showModelConfig: $showModelConfig)
                        .frame(minWidth: 400)
                    
                    Divider()
                        .background(Color.gray.opacity(0.2))
                    
                    // 右侧用户资料面板
                    UserProfileView(showModelConfig: $showModelConfig)
                }
                .frame(minWidth: 900, minHeight: 700)
                .background(Color(red: 0.97, green: 0.97, blue: 0.97))
            }
            
            // 欢迎页面
            if isWelcomeShown {
                VStack {
                    Spacer()
                    WelcomeView(isWelcomeShown: $isWelcomeShown)
                    Spacer()
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color.black.opacity(0.4))
                .edgesIgnoringSafeArea(.all)
            }
            
            // 模型配置弹窗
            if showModelConfig {
                ZStack {
                    // 半透明背景，点击时关闭弹窗
                    Rectangle()
                        .fill(Color.black.opacity(0.4))
                        .edgesIgnoringSafeArea(.all)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            print("背景被点击")
                            withAnimation {
                                showModelConfig = false
                            }
                        }
                    
                    // 配置视图居中显示
                    ModelConfigView(isPresented: $showModelConfig)
                        .transition(.scale)
                }
                .zIndex(3)
            }
        }
        .onAppear {
            print("ContentView出现，showModelConfig初始值: \(showModelConfig)")
        }
    }
}

#Preview {
    ContentView()
}
